package com.ln.lnzw.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ScreenUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by wuwei on 2018/1/8.
 * description :
 */
public class ImageUtil {
    /**
     * 根据屏幕宽度按比例设置控件大小
     *
     * @param view
     * @param width  宽比例
     * @param height 高比例
     */
    public static void suitDisplay3(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth() / 3;
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }

    public static void suitDisplay2(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth() / 2;
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }

    public static void suitDisplay1(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth()+20;
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }
    public static void cardSuitDisplay1(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth()-40;
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }
    public static void suitDisplayWideFill(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth();
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }

    /**
     * 九宫格图片大小
     *
     * @param view
     * @param width  宽比例
     * @param height 高比例
     */
    public static void suitDisplayNine(View view, int width, int height) {
        ViewGroup.LayoutParams layPara = view.getLayoutParams();
        layPara.width = ScreenUtils.getScreenWidth() / 3 - 40;
        layPara.height = layPara.width * height / width;
        view.setLayoutParams(layPara);
    }

    /**
     * 压缩图片（质量压缩）
     * @param bitmap
     */
    public static File compressImage(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 500) {  //循环判断如果压缩后图片是否大于500kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            options -= 10;//每次都减少10
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
//            long length = baos.toByteArray().length;
        }
//        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//        Date date = new Date(System.currentTimeMillis());
//        String filename = format.format(date);
        File file = createImagePathFile();
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        recycleBitmap(bitmap);
        return file;
    }

    public static void recycleBitmap(Bitmap... bitmaps) {
        if (bitmaps == null) {
            return;
        }
        for (Bitmap bm : bitmaps) {
            if (null != bm && !bm.isRecycled()) {
                bm.recycle();
            }
        }
    }

    private static File createImagePathFile() {
        return new File(Environment.getExternalStorageDirectory(),
                "user_feedback" + System.currentTimeMillis() + ".jpg");
    }

}
